﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class DeviceManager : MonoBehaviour
{
    public static DeviceManager Instance { get; private set; }

    public UnityEvent onDevChange { get; private set; } = new UnityEvent();

    List<DeviceBase> _allDevice = new List<DeviceBase>();

    void Awake()
    {
        Instance = this;
    }

    void OnDestroy()
    {
        if(Instance == this)
            Instance = null;
    }

    public void Reg(DeviceBase dev) 
    {
        _allDevice.Add(dev);
        onDevChange.Invoke();
    }
    public void UnReg(DeviceBase dev) 
    { 
        _allDevice.Remove(dev);
        onDevChange.Invoke();
    }

    public List<DeviceBase> GetAllDevices()
    {
        return new List<DeviceBase>(_allDevice);
    }

    public DeviceBase GetDevice(string type, string id)
    {
        return _allDevice.Find(n => { return n.GetDevType() == type && n.GetDevId() == id; });
    }
}
